package com.bio.action.task;

import java.util.Calendar;
import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.BeanUtils;

import com.bio.action.AbstractAction;
import com.bio.entity.task.Task;
import com.bio.entity.user.User;
import com.bio.service.task.TaskService;
import com.bio.service.user.UserService;
import com.bio.utils.Environment;

@Namespace("/task")
@Results( { 
		@Result(name = "list", location = "/WEB-INF/views/task/listTasks.jsp", type = "dispatcher"), 
		@Result(name = "add", location = "/WEB-INF/views/task/addTask.jsp", type = "dispatcher"),
		@Result(name = "tolist", location = "task!list", type = "redirect"),
		@Result(name = "edit", location = "/WEB-INF/views/task/editTask.jsp", type = "dispatcher")
})
public class TaskAction extends AbstractAction<Task, TaskService> {

	private static final long serialVersionUID = 5018553630561493055L;

	private Task task;
	
	private List<User> allChargePersons;
	
	private UserService userService;


	public TaskAction(TaskService service) {
		super(service);
	}
	
	public String add(){
		Long companyId = Environment.Session.getCompanyId();
		allChargePersons = this.userService.findPagerByCompanyId(null, companyId).getResult();
		return "add";
	}
	
	public String save(){
		task.setCreatedDate(Calendar.getInstance());
		task.setCreatorName(Environment.Session.getUserName());
		task.setCreator(Environment.Session.getUserId());
		service.save(task);
		return "tolist";
	}
	
	public String remove(){
		service.remove(id);
		return "tolist";
	}
	
	public String list(){
		pagination.setCurrentIndex(getCurrentIndex());
		pager = service.findPagerByUserId(pagination, Environment.Session.getUserId(), null);
		return "list";
	}
	
	public String toEdit(){
		Long companyId = Environment.Session.getCompanyId();
		allChargePersons = this.userService.findPagerByCompanyId(null, companyId).getResult();
		task = service.get(id);
		return "edit";
	}
	
	public String update(){
		Task oldTask = service.get(id);
		final String[] INGORED_PROPERTIES = {"ownerId", "startDate", "endDate", "status", "content"};
		BeanUtils.copyProperties(oldTask, task, INGORED_PROPERTIES);
		task.setUpdatedDate(Calendar.getInstance());
		task.setUpdater(Environment.Session.getUserId());
		service.save(task);
		return "tolist";
	}
	
	public void setTask(Task task) {
		this.task = task;
	}
	
	public void setAllChargePersons(List<User> allChargePersons) {
		this.allChargePersons = allChargePersons;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public Task getTask() {
		return task;
	}

	public List<User> getAllChargePersons() {
		return allChargePersons;
	}

	public UserService getUserService() {
		return userService;
	}
}
